package zmee.pmas.report;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.report.FilterInfo;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import zmee.util.DateUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yss
 * @createTime 2022-12-30 16:55
 * @description 项目报表插件
 */
public class ProBudgetFormPlugin extends AbstractReportFormPlugin implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        BasedataEdit edit=this.getControl("pro");
        edit.addBeforeF7SelectListener(this);
    }

    @Override
    public void setMergeColums(List<String> columns) {
        super.setMergeColums(columns);
        columns.add("zmee_pro");
    }

    @Override
    public void initDefaultQueryParam(ReportQueryParam queryParam) {
        Object[] org=new Object[]{RequestContext.get().getOrgId()};
        this.getModel().setValue("bosorg", org);
    }

  /*  @Override
    public void processRowData(String gridPK, DynamicObjectCollection rowData, ReportQueryParam queryParam) {
        FilterInfo filter = queryParam.getFilter();
        Date year = filter.getDate("year");
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(year);
        calendar.getTimeInMillis();
        int tear = calendar.get(Calendar.YEAR);//年
        Date beginYear = DateUtil.getBeginDayOfYear(tear);//本年开始时间
        Date endYear = DateUtil.getEndDayOfYear(tear);//本年结束时间

        QFilter qFYear=new QFilter("begindate", QCP.large_equals,beginYear)
                .and(new QFilter("enddate",QCP.less_equals,endYear));
        DynamicObject[] period = BusinessDataServiceHelper.load("bd_period", "id", qFYear.toArray());
        String[] perid=new String[period.length];
        for (int i = 0; i < period.length; i++) {
            perid[i]=period[i].getString("id");
        }

        for (DynamicObject row :rowData) {
            String orgid = row.getString("org.id");
            String projectid=row.getString("zmee_pro.id");
            String budget=row.getString("zmee_budget.id");

            QFilter qFilter=new QFilter("org.id", QCP.equals,orgid)
                    .and(new QFilter("contpro.id", QCP.equals,projectid))
                    .and(new QFilter("period.id", QCP.in,perid)
                    .and(new QFilter("billstatus", QCP.equals,"C")));
            //查询付款登记单
            DynamicObject[] loads = BusinessDataServiceHelper.load("pmct_paymentregister", "id", qFilter.toArray());
            BigDecimal big=new BigDecimal("0.00");
            for (DynamicObject load:loads) {
                DynamicObject paymentreg = BusinessDataServiceHelper.loadSingle(load.getPkValue(), load.getDynamicObjectType().getName());
                //付款信息分录
                DynamicObjectCollection entryentity = paymentreg.getDynamicObjectCollection("entryentity");
                for (DynamicObject ent:entryentity) {
                    String contpro = ent.getString("contpro.id");
                    if(projectid.equals(contpro)){
                        DynamicObjectCollection subentryentity = ent.getDynamicObjectCollection("subentryentity");
                        for (DynamicObject subent:subentryentity) {
                            //获取预算项目名称
                            String name = subent.getString("budgetitem.id");
                            if(budget.equals(name)){
                                //获取该预算项的分摊金额
                                big=big.add(subent.getBigDecimal("curactualpaysplitamt"));
                            }
                        }
                    }
                }
            }
            //本年支付金额
            row.set("zmee_yearpay",big);
        }
    }*/

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        if("pro".equals(name)){
            Object org = this.getModel().getValue("bosorg");
            if(org != null) {
                String ids = ids((DynamicObjectCollection) org);
                List<QFilter> list = new ArrayList<>();
                QFilter qFilter = new QFilter("useorg.id", QCP.in,ids);
                list.add(qFilter);
                event.setCustomQFilters(list);
            }
        }
    }
    private String ids(DynamicObjectCollection row){
        String proIds="";
        for (int i = 0; i < row.size(); i++) {
            if(!"".equals(proIds) && proIds != null){
                proIds=proIds+",";
            }
            proIds = proIds+row.get(i).getString("fbasedataid.id");
        }
        return proIds;
    }
}
